import Taro from '@tarojs/taro'
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { NoInternetEmpty } from '@COMPONENTS/Empty'
import { handleClickTab } from '@UTILS/common'

const withNoInternet = WrappedComponent => {
  @connect(
    state => ({ isConnected: state.auth.isConnected })
  )
  class NoInternet extends Component {
    onClick = () => {
      const reload = this.child?.fetchData
      reload?.()
    }

    // TODO 暂时不清楚，为什么只放在这里才生效，需要查一下
    onTabItemTap(item) {
      handleClickTab(`/${item.pagePath}`)
    }

    onShareAppMessage() {
      return this.child?.onShareAppMessage?.()
    }

    onShareTimeline() {
      return this.child?.onShareTimeline?.()
    }

    componentDidShow() {
      return this.child?.componentDidShow?.()
    }

    onAddToFavorites() {
      return this.child?.onAddToFavorites?.()
    }

    render() {
      const { isConnected } = this.props
      if (!isConnected) {
        return <NoInternetEmpty onClick={this.onClick} />
      }

      return (
        <WrappedComponent
          // ref={this.wrapped}
          getInstance={
            child => {
              if (child) {
                this.child = child
              }
            }
          }
          {...this.props}
        />
      )
    }
  }

  return NoInternet
}

export default withNoInternet
